package com.logistics.material.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.logistics.container.entity.domian.Container;
import com.logistics.container.service.ContainerService;
import com.logistics.erp.entity.domain.ErpFlow;
import com.logistics.erp.entity.domain.ErpOrder;
import com.logistics.erp.entity.model.ErpInvBillSave;
import com.logistics.erp.service.ErpInvBillService;
import com.logistics.erp.service.ErpOrderService;
import com.logistics.inventory.entity.domain.Inventory;
import com.logistics.inventory.entity.domain.InventoryDetail;
import com.logistics.inventory.entity.model.InventoryDetailSave;
import com.logistics.inventory.entity.model.InventorySave;
import com.logistics.inventory.service.InventoryDetailService;
import com.logistics.inventory.service.InventoryService;
import com.logistics.map.entity.domain.MapComponents;
import com.logistics.map.service.MapComponentsService;
import com.logistics.material.entity.model.MaterialPickRow;
import com.logistics.material.entity.model.MaterialPickSave;
import com.logistics.material.entity.model.WmsPickSave;
import com.logistics.material.mapper.MaterialMapper;
import com.logistics.pick.entity.domain.Pick;
import com.logistics.pick.entity.domain.PickContainer;
import com.logistics.pick.entity.domain.PickDetail;
import com.logistics.pick.entity.domain.PickDetailInvBill;
import com.logistics.pick.entity.model.PickDetailSave;
import com.logistics.pick.entity.model.PickRow;
import com.logistics.pick.entity.model.PickSave;
import com.logistics.pick.service.PickContainerService;
import com.logistics.pick.service.PickDetailService;
import com.logistics.pick.service.PickService;
import com.logistics.qrcode.service.QrcodeService;
import com.logistics.sequence.service.SequenceService;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.bean.UserBean;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.constants.Message;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.message.Manager;
import com.logistics.utils.tool.DateUtils;
import com.logistics.utils.tool.StringUtils;
import com.logistics.utils.tool.UserUtils;
import com.logistics.wms.entity.domain.WmsOrder;
import com.logistics.wms.service.WmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MaterialPickService {

    @Autowired
    private PickService pickService;

    @Autowired
    private ContainerService containerService;

    @Autowired
    private PickContainerService pickContainerService;

    @Autowired
    private PickDetailService pickDetailService;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private InventoryDetailService inventoryDetailService;

    @Autowired
    private ErpInvBillService erpInvbillService;

    @Autowired
    private WmsOrderService wmsOrderService;

    @Autowired
    private ErpOrderService erpOrderService;

    @Autowired
    private QrcodeService qrcodeService;

    @Autowired
    private MapComponentsService mapComponentsService;

    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UserUtils userUtils;

    /**
     * 物料拣配-查询物料信息
     *
     * @param code
     * @return
     */
    public MaterialPickRow material(String code, String billNo) throws MessageException {
        if (StringUtils.isNull(billNo)){
            // 判断是否是任务单号
            if (code.startsWith("800")) {
                WmsOrder result = wmsOrderService.getWmsOrderLabelNumber(new BigDecimal(code));
                // 查找物料信息
                ErpOrder order = erpOrderService.materialInfo(result.getDocumentnumber(),
                        result.getItemid(), null);
                // 查找已经拣配的数量
                BigDecimal pickTotal = pickDetailService.totalPickQty(order.getOtherBillNo(), order.getShopOrder(), order.getItemCode(), order.getOpNo(), result.getOId());
                order.setQty(order.getQty().subtract(pickTotal));
                order.setSeqNo(result.getOId());
                // 查找物料信息
                return MaterialPickRow.build(order);
            }
            ErpOrder order = erpOrderService.materialInfo(null, null, code);
            BigDecimal totalQty = pickDetailService.list(new LambdaQueryWrapper<PickDetail>()
                            .eq(PickDetail::getItemSeqNo, order.getId())).stream().map(PickDetail::getItemQty).
                    reduce(new BigDecimal(0), (a, b) -> a.add(b));
            order.setQty(order.getQty().subtract(totalQty));
            // 查找物料信息
            return MaterialPickRow.build(order);
        }else {//涂装拣配
            ErpOrder order = erpOrderService.findMaterialData(code, billNo);
            if (StringUtils.isNull(order)){
                throw new MessageException("未查询到物料信息");
            }
            // 查找已经拣配的数量
            BigDecimal pickTotal = pickDetailService.totalPickQty(order.getOtherBillNo(),
                    code, order.getItemCode(), order.getOpNo(), null);
            order.setQty(order.getQty().subtract(pickTotal));
            order.setShopOrder(code);
            order.setSeqNo("0");
            return MaterialPickRow.build(order);
        }
    }

    /**
     * 保存物料信息
     *
     * @param save
     */
    @Insert
    public void saveMaterial(MaterialPickSave save) throws MessageException {
        // 根据拣配单号会回写领料单号和优先级
        // 查找拣配单
        Pick resultPick = pickService.getOne(new LambdaQueryWrapper<Pick>().eq(Pick::getCode, save.getPickCode()));
        if (StringUtils.isNull(resultPick)) {
            throw new MessageException("拣配单不存在");
        }
        if (new BigDecimal(save.getItemQty()).compareTo(new BigDecimal(0)) == 0){
            throw new MessageException("物料已全部拣配");
        }

        // 查询优先级
        Map<String, Object> resultMap = erpOrderService.priority(save.getErpInvBillCode());
        ErpFlow flow = null;
        if (StringUtils.isNotNull(save.getLocation())){//涂装拣配
            flow = ErpFlow.build(save);
            resultMap.put("LOCATION", save.getLocation());
        } else if(StringUtils.isNull(save.getEndPointCode())){
            // 通过任务号查询流向信息
            flow = erpOrderService.flow(save.getTaskNo(), save.getItemOpNo());
        } else {
            flow = ErpFlow.build(save);
        }
        // 物料区域
        String locationArea =  null;
        if(StringUtils.isNotNull(resultMap)){
            //获取物料区域
            locationArea = StringUtils.isNotNull(resultMap.get("LOCATION")) ? resultMap.get("LOCATION").toString() : null;
        }
        List<PickDetailInvBill> materials = pickDetailService.materials(PickDetailInvBill.build(resultPick.getId()));
        if(!materials.isEmpty()){
            if(StringUtils.isNotNull(save.getErpInvBillCode())
                    && StringUtils.isEquals(materials.get(0).getErpInvBillType(), Constants.PICK_TYPE_NOT_CUBE)){
                throw new MessageException("立体库单据不能和非立体库单据同时拣配");
            }else if(StringUtils.isNull(save.getErpInvBillCode())
                    && StringUtils.isEquals(materials.get(0).getErpInvBillType(), Constants.PICK_TYPE_DELIVERY)){
                throw new MessageException("非立体库单据不能和立体库单据同时拣配");
            }

            //通过托盘编号和拣配单查询
            List<PickDetailInvBill> materialContainers = pickDetailService.materials(PickDetailInvBill.build(resultPick.getId(),save.getContainerCode()));
            // 区域判断
            if (!materialContainers.isEmpty() && StringUtils.isNotNull(save.getErpInvBillCode())){
                //判断dept 以及对应字段是否与保存物料一致
                if (StringUtils.isEquals(flow.getDeptCodeWc(), Constants.WC_CODE_ASSEMBLE)) {

                    if (StringUtils.isNull(materialContainers.get(0).getLocationArea())){
                        throw new MessageException("APS未给物料指定区域");
                    }

                    if(!StringUtils.isEquals(materialContainers.get(0).getLocationArea(), locationArea)){
                        throw new MessageException("不同区域物料不能拣配到同一个托盘");
                    }
                }
            }
        }
        if(StringUtils.isNull(save.getErpInvBillCode())){
            BigDecimal totalQty = pickDetailService.list(new LambdaQueryWrapper<PickDetail>()
                    .eq(PickDetail::getItemSeqNo, save.getItemSeqNo())).stream().map(PickDetail::getItemQty).
                    reduce(new BigDecimal(0), (a, b) -> a.add(b));
            if(new BigDecimal(save.getItemQty()).compareTo(totalQty) < 0){
                throw new MessageException("物料已经拣配过了，不能重复拣配");
            }
            save.setErpInvBillCode(resultPick.getCode());
        }
//        else{
//            WmsOrder wmsOrder = wmsOrderService.getWmsOrderLabelNumber(new BigDecimal(save.getCode()));
//            save.setItemSeqNo(wmsOrder.getOId());
//            // 已拣配数量
//            BigDecimal totalQty = pickDetailService.totalPickQty(save.getErpInvBillCode(), save.getTaskNo(), save.getItemCode(), save.getItemOpNo(), wmsOrder.getOId());
//
//            // 查找物料信息
//            ErpOrder order = erpOrderService.materialInfo(wmsOrder.getDocumentnumber(),
//                    wmsOrder.getItemid(), null);
////            // ERP拣配实际数量
////            BigDecimal actualQty = erpOrderService.actualQty(save.getErpInvBillCode(), save.getTaskNo(), save.getItemCode(), save.getItemOpNo());
//            if(new BigDecimal(save.getItemQty()).compareTo(order.getQty().subtract(totalQty)) > 0){
//                throw new MessageException("物料已经拣配过了，不能重复拣配");
//            }
//        }
        // 验证物料是否在同一班组下
        PickContainer pickContainer = pickContainerService.getOne(new LambdaQueryWrapper<PickContainer>()
                .eq(PickContainer::getContainer, save.getContainerCode())
                .eq(PickContainer::getStatus, Constants.PICK_STATE_IN)
                .eq(PickContainer::getPick, resultPick.getId()));
        String wcCode = StringUtils.isNotNull(save.getEndPointCode()) ? save.getEndPointCode() : flow.getWcCode();
        if(StringUtils.isNotNull(pickContainer) && StringUtils.isNotNull(pickContainer.getWcCode()) &&
                !StringUtils.isEquals(pickContainer.getWcCode(), wcCode)){
            throw new MessageException("不同班组的物料不能拣配到同一托盘上");
        }
        // 拣配单类型 配送单
        if (StringUtils.isNotNull(save.getLocation())){
            save.setErpInvBillType(Constants.PICK_TYPE_PAINTING);
        }else{
            save.setErpInvBillType(StringUtils.isNotNull(save.getEndPointCode()) ? Constants.PICK_TYPE_NOT_CUBE : Constants.PICK_TYPE_DELIVERY);
        }

        // 检查物料是否存在
        updateMaterial(save, flow, resultMap);
    }

    /**
     * 根据上面查询结果-控制事务保存物料信息
     *
     * @param save
     * @param resultFlow
     * @param resultMap
     * @throws MessageException
     */
    @Transactional
    public void updateMaterial(MaterialPickSave save, ErpFlow resultFlow, Map<String, Object> resultMap) throws MessageException {
        // 回写pick表 拣配单
        Integer priority =  null;
        String locationArea =  null;
        if(StringUtils.isNotNull(resultMap)){
            priority =  StringUtils.isNotNull(resultMap.get("PRIORITY")) ? Integer.parseInt(resultMap.get("PRIORITY").toString()) : null;
            locationArea = StringUtils.isNotNull(resultMap.get("LOCATION")) ? resultMap.get("LOCATION").toString() : null;
        }
        Integer invBill = erpInvbillService.save(ErpInvBillSave.build(save, resultFlow, Constants.LACK_MATERIAL_STATUS_NO, priority, locationArea));
        // 保存拣配单物料信息信息
        pickDetailService.save(PickDetail.build(save, invBill));
        // 更新pickContainer表的班组信息
        String wcCode = StringUtils.isNotNull(resultFlow.getEndLocation()) ? resultFlow.getEndLocation() : resultFlow.getWcCode();
        String wcName = StringUtils.isNotNull(resultFlow.getEndLocationName()) ? resultFlow.getEndLocationName() : resultFlow.getWcName();
        pickContainerService.update(PickContainer.build(wcCode, wcName),
                new LambdaQueryWrapper<PickContainer>().
                        eq(PickContainer::getContainer, save.getContainerCode())
                        .eq(PickContainer::getStatus, Constants.PICK_STATE_IN));
        // 根据托盘号和物料编码判断库存详细信息是否存在 不存在新增 否则更新数量
        inventoryDetailService.merge(InventoryDetailSave.build(save));
        // 更新库存表托盘状态
        inventoryService.update(Inventory.build(Constants.CONTAINER_IS_EMPTY_NO),
                new LambdaQueryWrapper<Inventory>().eq(Inventory::getContainer,  save.getContainerCode()));
    }

    /**
     * 查找物料列表
     *
     * @param pickId
     * @return
     */
    public List<MaterialPickRow> listByPickId(String pickId, String container) {
        return pickDetailService.list(new LambdaQueryWrapper<PickDetail>().eq(PickDetail::getPick, pickId).
                        eq(PickDetail::getContainer, container)
                        .orderByDesc(PickDetail::getCreateTime))
                .stream().map(e -> MaterialPickRow.build(e)).collect(Collectors.toList());
    }

    /**
     * 添加托盘
     * @param params
     * @return
     * @throws MessageException
     */
    @Insert
    @Transactional
    public PickRow createContainer(PickSave params) throws MessageException {
        UserBean userBean = userUtils.getUserInfo();
        if (StringUtils.isNull(params.getContainerCode())) {
            throw new MessageException(Manager.getMessage(Message.MSG1013.getCode(), "托盘"));
        }
        // 校验主数据
        Container container = containerService.queryContainer(params.getContainerCode());
        String point = "";
        Pick result = pickService.getPickContainer(params.getContainerCode(), params.getPickCode());
        if(StringUtils.isNotNull(params.getStation())){
            point = qrcodeService.valueByCode(params.getStation(), Constants.CODE_TYPE_LOCATION);
            PickContainer pickContainer = pickContainerService.occupiedSite(point);
            if (StringUtils.isNotNull(pickContainer)){
                throw new MessageException("该站点已经被托盘:"+ pickContainer.getContainer() +"绑定");
            }
            MapComponents location = mapComponentsService.getOne(new LambdaQueryWrapper<MapComponents>()
                    .eq(MapComponents::getLocation, point));
            if(StringUtils.isNull(location)){
                throw new MessageException("站点不正确");
            }
            if(!StringUtils.isEquals(location.getContainerType(), container.getType())){
                throw new MessageException("站点类型和托盘类型不匹配");
            }
            if (StringUtils.isEquals(location.getLocationArea(), Constants.LOCATION_AREA_LTK)){
                // 查找拣配优先级
                Integer priority = pickDetailService.isPriority(params.getContainerId());
                if(StringUtils.isNotNull(priority)){
                    String tray = materialMapper.maxPrioritytray(priority.toString());
                    throw new MessageException("该领料单不是最高优先级，最高优先级为："+priority+" 托盘:"+tray);
                }
            }
        }
        if (StringUtils.isNotNull(params.getContainerId())) {
            // 变更托盘位置
            pickContainerService.update(PickContainer.build(point),
                    new LambdaQueryWrapper<PickContainer>().eq(PickContainer::getId, params.getContainerId()));
//            // 更新站点位置
            inventoryService.update(Inventory.build(point), new LambdaQueryWrapper<Inventory>().
                    eq(Inventory::getContainer, params.getContainerCode()));
            result.setContainers(pickService.getPickContainerList(result.getId()));
            // 查找托盘信息
            return PickRow.build(result.getCode(), result.getStatus(), result.getId(), result.getContainers());
        }
        // 更新库存主数据 库存存在更新 不存在添加
        params.setStation(point);
        // 添加库存表数据
        inventoryService.merge(InventorySave.build(params));
        if (StringUtils.isNotNull(result)) {
            if (StringUtils.isNotNull(params.getPickCode())) {
                // 保存托盘信息
                saveContainer(PickContainer.build(result.getId(), Constants.PICK_STATE_IN, params.getContainerCode(), point));
                result.setContainers(pickService.getPickContainerList(result.getId()));
            }
            boolean isShow = true;
            if(StringUtils.isEquals(userBean.getErpDeptCode(), Constants.WC_CODE_ASSEMBLE)){
                Integer count = pickDetailService.findAssemblyByBillNo(result.getId());
                isShow = count > 0;
            }
            // 查找托盘信息
            return PickRow.build(result.getCode(), result.getStatus(), result.getId(), result.getContainers(), isShow);
        } else {
//            if (!Constants.PICK_STATE_IN.equals(result.getStatus().toString())
//                    && !Constants.PICK_STATE_SUCCESS.equals(result.getStatus().toString())) {
//                // 该托盘 在lgs_pick_container表中没有状态是 0-拣配中 10-拣配完成 的任务时，清空托盘绑定的物料
//                pickDetailService.remove(new LambdaQueryWrapper<PickDetail>().eq(PickDetail::getPick, params.getPickCode())
//                        .eq(PickDetail::getContainer, params.getContainerCode()));
//            }
            // 下列是添加拣配单业务
            String seqNo = sequenceService.getCode("J" + DateUtils.dateToStringFormat(new Date(), DateUtils.YYYYMMDD), 4);
            String code = "J" + DateUtils.dateToStringFormat(new Date(), DateUtils.YYYYMMDD) + seqNo;
            result = Pick.build(params);
            result.setCode(code);
            result.setStatus(StringUtils.isNotNull(params.getStatus()) ?
                    Integer.parseInt(params.getStatus()) :
                    Integer.parseInt(Constants.PICK_STATE_IN));
            pickService.save(result);
            // 保存托盘信息
            saveContainer(PickContainer.build(result.getId(),
                    StringUtils.isNotNull(params.getStatus()) ? params.getStatus() :
                            Constants.PICK_STATE_IN, params.getContainerCode(), point));
            result.setContainers(pickService.getPickContainerList(result.getId()));
        }
        return PickRow.build(result.getCode(), result.getStatus(), result.getId(), result.getContainers());
    }

    /**
     * 保存托盘信息
     *
     * @param pickContainer
     */
    private void saveContainer(PickContainer pickContainer) throws MessageException {
        PickContainer result = pickContainerService.getOne(new LambdaQueryWrapper<PickContainer>().
                in(PickContainer::getStatus, Arrays.asList(Constants.PICK_STATE_IN,
                        Constants.PICK_STATE_SUCCESS, Constants.PICK_STATE_DELIVERY_IN)).
                eq(PickContainer::getContainer, pickContainer.getContainer()));
        if (StringUtils.isNotNull(result)) {
            throw new MessageException("托盘没完成配送，不能重复添加");
        }

        List<PickContainer> pickingList = pickContainerService.list(new LambdaQueryWrapper<PickContainer>().
                in(PickContainer::getStatus, Arrays.asList(Constants.PICK_STATE_IN, Constants.PICK_STATE_SUCCESS)).
                eq(PickContainer::getContainer, pickContainer.getContainer()));
        if (pickingList.isEmpty()) {
            // 该托盘 在lgs_pick_container表中没有状态是 0-拣配中 10-拣配完成 的任务时，清空托盘绑定的物料
            inventoryDetailService.remove(new LambdaQueryWrapper<InventoryDetail>().eq(InventoryDetail::getContainer, pickContainer.getContainer()));
        }
        pickContainerService.save(pickContainer);
    }

    /**
     * 删除托盘
     *
     * @param ids
     * @return
     */
    @Transactional
    public void removeContainer(String ids) throws MessageException {
        if (StringUtils.isNull(ids)) {
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        List<String> idList = StringUtils.splitToList(ids, ",");
        List<PickContainer> resultPicks = pickContainerService.list(new LambdaQueryWrapper<PickContainer>().in(PickContainer::getId, idList));
        // 删除托盘
        pickContainerService.remove(new LambdaQueryWrapper<PickContainer>().in(PickContainer::getId, idList));
        // 根据单号和托盘编号 删除物料信息
        List<Integer> pickId = resultPicks.stream().map(PickContainer::getPick).collect(Collectors.toList());
        List<String> container = resultPicks.stream().map(PickContainer::getContainer).collect(Collectors.toList());
        pickDetailService.remove(new LambdaQueryWrapper<PickDetail>().in(PickDetail::getPick, pickId).
                in(PickDetail::getContainer, container));
        // 更新库存表托盘状态
        inventoryService.update(Inventory.build(Constants.CONTAINER_IS_EMPTY_YES),
                new LambdaQueryWrapper<Inventory>().eq(Inventory::getContainer, container));
    }

    /**
     * 更新数量（拣配单详细、库存详细）
     *
     * @param params 参数
     */
    @Insert
    @Transactional
    public void updateItemQty(PickDetailSave params) throws MessageException {
        if (StringUtils.isNull(params.getItemQty())) {
            throw new MessageException(Manager.getMessage(Message.MSG1013.getCode(), "数量"));
        } else if (StringUtils.isNull(params.getId())) {
            throw new MessageException(Manager.getMessage(Message.MSG1013.getCode(), "拣配单详细id"));
        }

        PickDetail pickDetail = pickDetailService.getById(Integer.parseInt(params.getId()));
        if (pickDetail != null) {
            // 拣配单详细
            pickDetailService.update(PickDetail.build(params.getItemQty()),
                    new LambdaQueryWrapper<PickDetail>().eq(PickDetail::getId, params.getId()));
            InventoryDetail save = InventoryDetail.build(params);
            save.setItemQty(new BigDecimal(params.getItemQty()));
            // 库存详细
            inventoryDetailService.update(save, new LambdaUpdateWrapper<InventoryDetail>()
                    .eq(InventoryDetail::getContainer, pickDetail.getContainer())
                    .eq(InventoryDetail::getItemCode, pickDetail.getItemCode()));
        } else {
            throw new MessageException(Manager.getMessage(Message.MSG1021.getCode(), "拣配单详细"));
        }
    }

    /**
     * 移除物料
     *
     * @param ids 参数
     */
    @Transactional
    public void removeMaterial(String ids) throws MessageException {
        if (StringUtils.isNull(ids)) {
            throw new MessageException(Manager.getMessage(Message.MSG1013.getCode(), "拣配单详细id"));
        }
        List<String> idList = StringUtils.splitToList(ids, ",");
        // 拣配单详细
        List<PickDetail> pickDetails = pickDetailService.list(new LambdaQueryWrapper<PickDetail>()
                .in(PickDetail::getId, idList));
        // 根据托盘和物料编码查找库存
        List<String> containers = pickDetails.stream().map(PickDetail::getContainer).collect(Collectors.toList());
        List<String> itemCodes = pickDetails.stream().map(PickDetail::getItemCode).collect(Collectors.toList());
        inventoryDetailService.remove(new LambdaQueryWrapper<InventoryDetail>().
                in(InventoryDetail::getContainer, containers).in(InventoryDetail::getItemCode, itemCodes));
        // 拣配单详细
        pickDetailService.removeByIds(idList);
        erpInvbillService.removeByIds(pickDetails.stream().map(PickDetail::getInvbill).collect(Collectors.toList()));
        if(!pickDetails.isEmpty()){
            List<PickDetail> list = pickDetailService.list(new LambdaQueryWrapper<PickDetail>().in(PickDetail::getPick, pickDetails.get(0).getPick())
                    .in(PickDetail::getContainer, containers));
            if(list.isEmpty()){
                pickContainerService.update(PickContainer.build("", ""), new LambdaQueryWrapper<PickContainer>()
                        .in(PickContainer::getContainer, containers));
            }
        }
    }

    /**
     * wms拣配
     */
    @Insert
    @Transactional
    public void wmsPick(WmsPickSave params) throws MessageException {
        if(StringUtils.isNull(params.getContainerCode())){
            throw new MessageException("托盘编号不能为空");
        }
        if (StringUtils.isNull(params.getStation())){
            throw new MessageException("站点不能为空");
        }
        if(StringUtils.isNull(params.getMaterials())){
            throw new MessageException("物料不能为空");
        }
        PickSave pickSave = new PickSave();
        pickSave.setContainerCode(params.getContainerCode());
        pickSave.setStation(params.getStation());
        pickSave.setCreateDepCode(params.getCreateDepCode());
        pickSave.setCreateOrgCode(params.getCreateOrgCode());
        pickSave.setCreator(params.getCreator());
        pickSave.setCreateTime(params.getCreateTime());
        pickSave.setUpdateTime(params.getUpdateTime());
        pickSave.setUpdater(params.getUpdater());
        // 保存托盘
        PickRow result = createContainer(pickSave);
        // 保存物料
        for(MaterialPickSave item : params.getMaterials()){
            item.setPickId(result.getId().toString());
            item.setContainerCode(params.getContainerCode());
            item.setPickCode(result.getCode());
            item.setStation(params.getStation());
            item.setCreateDepCode(params.getCreateDepCode());
            item.setCreateOrgCode(params.getCreateOrgCode());
            item.setCreator(params.getCreator());
            item.setCreateTime(params.getCreateTime());
            item.setUpdateTime(params.getUpdateTime());
            item.setUpdater(params.getUpdater());
            saveMaterial(item);
        }
        // 拣配完成
        pickSave.setId(result.getId().toString());
        pickService.finishPick(pickSave);
    }

}
